package com.dahua.messagesyncsh.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.dahua.messagesyncsh.common.Constants;
import com.dahua.messagesyncsh.config.redis.RedisUtil;
import com.dahua.messagesyncsh.criteria.CollectionQueryCriteria;
import com.dahua.messagesyncsh.kafka.enums.Fields;
import com.dahua.messagesyncsh.entity.Collection;
import com.dahua.messagesyncsh.mapper.CollectionMapper;
import com.dahua.messagesyncsh.service.CollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class CollectionServiceImpl implements CollectionService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private CollectionMapper collectionMapper;

    @Override
    public void caching() {
        List<Collection> enquire = collectionMapper.enquire(new CollectionQueryCriteria());

        if (enquire.isEmpty()){
            return;
        }

        for (Collection collection : enquire){
            String key = collection.getCollection() + collection.getSensor();
            redisUtil.setHash(Constants.COLLECTION_INFO, key , collection);
        }
    }


    @Override
    public List<Collection> enquire(CollectionQueryCriteria criteria) {

        if (criteria.getSensor() == null || criteria.getCollection() == null) {
            return collectionMapper.enquire(criteria);
        }
        /*  缓存*/
        String key = criteria.getCollection() + criteria.getSensor();
        Object hash = redisUtil.getHash(Constants.COLLECTION_INFO, key);

        if (hash != null) {
            if (hash.equals(Constants.NULL)) {
                return Collections.emptyList();
            } else {
                return Collections.singletonList(JSON.parseObject(JSON.toJSONString(hash), Collection.class));
            }
        }
        List<Collection> enquire = collectionMapper.enquire(criteria);
        if (enquire != null && enquire.size() == 1) {
            Collection collection = enquire.get(0);
            if (collection.getMapFields() != null) {
                String[] fields = collection.getMapFields().split(";");

                for (String value : fields) {
                    Fields field = null;
                    try {
                        field = Fields.valueOf(value);
                    } catch (Exception e) {
                        log.error("【字段不存在】：{}", value);
                    }
                    if (field != null) {
                        if (collection.getFields() == null){
                            collection.setFields(Collections.singletonList(field));
                        }else{
                            collection.getFields().add(field);
                        }
                    }
                }
            }

            redisUtil.setHash(Constants.COLLECTION_INFO, key, collection);
            return enquire;
        } else {
            redisUtil.setHash(Constants.COLLECTION_INFO, key, Constants.NULL);
            log.error("【类型测点已设null】：{}", key);
            return Collections.emptyList();
        }
    }
}
